package models

import (
	"gorm.io/gorm"
	"learn-srv/basic/config"
)

type User struct {
	gorm.Model
	Username string `json:"username" gorm:"type:varchar(30);comment:'用户名'"`
	Password string `json:"password" gorm:"type:varchar(32);comment:'密码'"`
	UType    int    `json:"u_type" gorm:"type:int;comment:'类型: 1-管理员 2-商家 3-普通用户'"`
}

func (u *User) TableName() string {
	return "user"
}

func (u *User) CreateUser() error {
	return config.DB.Create(&u).Error
}

func (u *User) GetUserInfo(username string) error {
	return config.DB.Model(&User{}).Where("username = ?", username).Limit(1).Find(&u).Error
}

func (u *User) GetUserById(id int) error {
	return config.DB.Model(&User{}).Where("id = ?", id).Limit(1).Find(&u).Error
}
